#include "qemenu.h"
#include <iostream>

Element::Element(QWidget * parent) : QAbstractButton(parent)
{
     this->setGeometry(0,0,380,50);
     show();
     last = true;
}

void Element::setLast(bool Last) {
    last = Last;
    update();
}

bool Element::isLast(){
    return last;
}

void Element::paintEvent(QPaintEvent * event){
    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    QFont font;
    font.setBold(true);
    font.setPointSize(22);

    QColor txt(42,212,255,255);

    p.setFont(font);
    p.setPen(txt);
    p.setBrush(txt);

    p.save();
    p.drawText(0,5,380,p.fontMetrics().height()+3, Qt::AlignCenter, text());
    p.restore();

    if (!last) {
        QLinearGradient  lineG(0,0,360,0);
        lineG.setColorAt(0.0,QColor(42,212,255,0));
        lineG.setColorAt(0.5,QColor(42,212,255,255));
        lineG.setColorAt(1.0,QColor(42,212,255,0));

        p.setPen(Qt::NoPen);
        p.setBrush(lineG);

        p.save();
        p.drawRect(10,47,360,3);
        p.restore();
    }

    p.end();

}


List::List(QWidget * parent) : QWidget(parent){
    count = 0;
    width=380;
    height=50;
}

int List::Width(){
    return width;
}

int List::Height(){
    return height;
}

void List::addElement(Element * E) {        void clicked(int Position);
    connect(E,SIGNAL(clicked(int)), this, SLOT(elementClicked(int)));
    list.push_back(E);
    if (list.size()>1)
        for (int i = 0; i< list.size()-1; i++) list.at(i)->setLast(false);
    count ++;
    update();
}

int List::Count(){
    return count;
}


void List::paintEvent(QPaintEvent * event) {
    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    int h, w;

 /*   QFont font;
    font.setBold(true);
    font.setPointSize(28);

    p.setFont(font);*/

    h = count*(50)+15;
    w=380;
    width=w;
    height=h;
    this->setGeometry(this->x(), 100, w, h);

    QColor fg(245,245,245,255);

    p.setBrush(fg);
    p.setPen(fg);

    p.save();
    p.drawRoundedRect(2,2,w-4,h-4,12,12);
    p.restore();

    p.end();
}

void List::clearList(){

    for (int i=0; i<list.size(); i++) {
       delete list.at(i);

    }
    list.clear();
    count= 0;
    update();
}

void List::removeElement(int at){
    delete list.at(at);
    list.removeAt(at);
    if (list.size()>1)
        for (int i = 0; i< list.size()-1; i++) list.at(i)->setLast(false);
    list.at(list.size()-1)->setLast(true);
    count --;
    update();
}

Back::Back(QWidget * parent) : QAbstractButton(parent)
{}

void Back::paintEvent(QPaintEvent * event) {
    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);


    QColor bg(0, 0, 0, 190);

    p.setBrush(bg);
    p.setPen(bg);

    p.save();
    p.drawRect(0,0,this->width(), this->height());
    p.restore();

    p.end();
}


QEMenu::QEMenu(QWidget * parent) : QWidget(parent)
{
    bg = new Back(this);
    bg->setGeometry(0,0,480,640);
    connect(bg , SIGNAL(clicked()), this, SLOT(hide()));
    list = new List(this);
    list->setGeometry(50,100,list->Width(), list->Height());
   /* QPainter p;
    QFont font;
    font.setBold(true);
    font.setPointSize(28);
    p.setFont(font);

    H = p.fontMetrics().height();*/

    connect(list, SIGNAL(clicked(int)), this, SLOT(elementClicked(int)));
}

QEMenu::~QEMenu(){
    list->clearList();
    delete list;
    delete bg;
}

void QEMenu::addItem(QAction *item){
    Element * E;
    E =new Element(list);
    E->setText(item->text());
    E->setGeometry(0,10+50*list->Count(),380,50);
    list->addElement(E);
    connect(E, SIGNAL(clicked()), item, SLOT(trigger()));
    list->update();
}

void QEMenu::paintEvent(QPaintEvent *event) {
        QPainter p(this);
        p.setRenderHint(QPainter::TextAntialiasing);
        p.setRenderHint(QPainter::Antialiasing);
        p.end();
}

void QEMenu::clearList(){
    list->clearList();
}

void QEMenu::GoHide(){
    hide();
}
